<template>
<div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/admin/category-list' }">分类管理</el-breadcrumb-item>
    <el-breadcrumb-item>添加分类</el-breadcrumb-item>
  </el-breadcrumb>
  <el-divider></el-divider>
  <el-form :model="category" :rules="rules" ref="category" label-width="100px" class="demo-ruleForm">
    <el-form-item label="类别名称" prop="categoryName">
      <el-input v-model="category.categoryName"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('category')">添加</el-button>
      <el-button @click="resetForm('category')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
export default {
  data() {
    return {
      category: {
        categoryName: '',
      },rules: {
        categoryName: [
          {required: true, message: '请输入类别名称', trigger: 'blur'},
          {min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur'}
        ]
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url="http://localhost:8080/categories/add-new";
          let category=this.qs.stringify(this.category,{arrayFormat:"repeat"})
          this.axios.post(url,category).then((response)=>{
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加类别成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

